package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.lease.LeaseDemandListEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.view.ui.fragment.self.LeaseDemandStateFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LeaseDemandStateModel extends BaseViewModel {
    public ObservableField<Map> map = new ObservableField<>(new HashMap());
    private LeaseDemandStateFragment fragment;
    private int pageNum = 1;
    public int pageSize = 10;

    public LeaseDemandStateModel(LeaseDemandStateFragment fragment) {
        this.fragment = fragment;
    }

    //快捷需求-列表
    public void getLeaseOrederListModel(boolean isLoadMore) {
        pageNum = isLoadMore ? pageNum + 1 : 1;
        map.get().put("pageNum", pageNum);
        map.get().put("pageSize", pageSize);
        APIManager.getInstance().getLeaseAPI().getPersonalList(map.get())
                .enqueue(new Callback<BaseEntity<ListResponse<List<LeaseDemandListEntity>>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<ListResponse<List<LeaseDemandListEntity>>>> call, Response<BaseEntity<ListResponse<List<LeaseDemandListEntity>>>> response) {
                        if (response.body() == null) return;
                        if (APIManager.RESPONSE_OK.equals(response.body().getState()) && response.body().getData() != null) {
                            fragment.showList(response.body().getData().getList(), isLoadMore);
                        } else {
                            assert response.body() != null;
                            fragment.onLoadComplete(isLoadMore);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<ListResponse<List<LeaseDemandListEntity>>>> call, Throwable t) {
                        fragment.onLoadComplete(isLoadMore);
                    }
                });
    }


    //快捷需求-删除
    public void delModel(String id) {
        APIManager.getInstance().getLeaseAPI().deleteDemand(id)
                .enqueue(new Callback<BaseEntity>() {
                    @Override
                    public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null) return;
                        if (response.body().getState().equals("ok")) {
                            MToast.showToast(fragment.getActivity(), "删除成功");
                            getLeaseOrederListModel(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity> call, Throwable t) {
                    }
                });
    }


}
